package com.wyh.controller.g3controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.wyh.common.result.Result;
import com.wyh.domain.dto.g3dto.CustomerPayDetailDto;
import com.wyh.domain.po.g3po.CustomerPayDetail;
import com.wyh.domain.vo.g3vo.CustomerBuildingVo;
import com.wyh.domain.vo.g3vo.CustomerPayDetailVo;
import com.wyh.service.g3service.ICustomerBuildingService;
import com.wyh.utils.g3utils.ProjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("/customerArrears")
@Api(tags = "欠费客户查询")
public class CustomerArrearsController {
    @Autowired
    private ICustomerBuildingService service;
    private CustomerPayDetailVo customerPayDetailVo = null;

    @ApiOperation("查询所选楼盘下的信息")
    @GetMapping("/selectAll/{ids}")
    public Result<CustomerBuildingVo> selectAll(@PathVariable List<Integer> ids) {
        CustomerBuildingVo customerBuildingVo = service.selectCustomerBuilding(ids);
        return Result.success(customerBuildingVo);

    }

    @ApiOperation("查询指定楼盘下顾客的欠款信息")
    @PostMapping("/selectCustomerPay")
    public Result<CustomerPayDetailVo> selectCustomerPay(@ApiParam("欠款用户信息查询条件")@RequestBody CustomerPayDetailDto customerPayDetailDto) {

        try {
            customerPayDetailVo = service.selectCustomerPayDetail(customerPayDetailDto);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

        return Result.success(customerPayDetailVo);
    }

    @ApiOperation("导出excel")
    @GetMapping("/export/excel")
    public void export(HttpServletResponse response) {
        if (customerPayDetailVo == null) {
            throw new RuntimeException("请先查询数据");
        }
        try {
            ProjectUtils.setExcelResponseProp(response, "客户欠费明细表");
            CustomerPayDetailVo records = customerPayDetailVo;
            EasyExcel.write(response.getOutputStream())
                    .head(CustomerPayDetail.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("客户欠费明细表")
                    .doWrite(records.getList());

            } catch (IOException e) {
                e.printStackTrace();
        }

    }
}
